package com.aoxin.service.impl.attendance;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.aoxin.mapper.attendance.SlfClassRecordMapper;
import com.aoxin.model.attendance.SlfClassRecord;
import com.aoxin.model.dto.ClassRecordDto;
import com.aoxin.service.inter.attendance.ClassRecordService;
import com.aoxin.util.ErrorUtil;
import com.aoxin.util.enumerate.AttendanceType;

@Service("classRecordService")
public class ClassRecordServiceImpl implements ClassRecordService {

	@Autowired
	private SlfClassRecordMapper slfClassRecordMapper;

	@Override
	@Transactional
	public Integer insertClassRecord(SlfClassRecord classRecord) {
		Assert.notNull(classRecord, String.format(ErrorUtil.PARAMETER_NULL, "classRecord"));
		Integer classRecordId = 0;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("syllabusId", classRecord.getSyllabusId());
		List<SlfClassRecord> classRecords = this.slfClassRecordMapper.getList(map);
		if (classRecords.size() < 1) {
			classRecord.setCreateDate(new Date());
			classRecord.setAttendance(AttendanceType.NORMAL.getId());
			this.slfClassRecordMapper.insertSelective(classRecord);
			classRecordId = classRecord.getRecordId();
		} else {
			classRecordId = classRecords.get(0).getRecordId();
		}
		return classRecordId;
	}

	@Override
	@Transactional
	public void deleteClassRecords(List<Integer> classRecordIds) {
		Assert.notEmpty(classRecordIds, String.format(ErrorUtil.PARAMETER_NULL, "classRecordIds"));
		this.slfClassRecordMapper.deleteByPrimaryKeys(classRecordIds);
	}

	@Override
	@Transactional
	public void updateClassRecord(SlfClassRecord classRecord) {
		Assert.notNull(classRecord, String.format(ErrorUtil.PARAMETER_NULL, "classRecord"));
		this.slfClassRecordMapper.updateByPrimaryKeySelective(classRecord);
	}

	@Override
	public SlfClassRecord getClassRecord(Integer classRecordId) {
		Assert.notNull(classRecordId, String.format(ErrorUtil.PARAMETER_NULL, "classRecordId"));
		return this.slfClassRecordMapper.selectByPrimaryKey(classRecordId);
	}

	@Override
	public List<SlfClassRecord> getClassRecordList(Map<String, Object> map) {
		return this.slfClassRecordMapper.getList(map);
	}

	@Override
	public List<ClassRecordDto> getClassRecordListByUser(Integer userId) {
		Assert.notNull(userId, String.format(ErrorUtil.PARAMETER_NULL, "userId"));
		// 统计上课记录
		List<ClassRecordDto> classRecordDtos = this.slfClassRecordMapper.getCountClassRecord(userId);
		Map<String, Object> map = new HashMap<String, Object>();
		for (ClassRecordDto classRecordDto : classRecordDtos) {
			// 查询每个孩子的上课记录
			map.put("childrenId", classRecordDto.getChildrenId());
			List<SlfClassRecord> slfClassRecords = this.slfClassRecordMapper.getList(map);
			classRecordDto.setClassRecords(slfClassRecords);
		}
		return classRecordDtos;
	}

}
